<template>
	<view class="container">
		<xy-popup-agreement></xy-popup-agreement>
		<uni-nav-bar
			left-icon=""
			:status-bar="true"
			:fixed="true"
			:border="true"
			:shadow="false">
			<view slot="left">
			</view>
			<view class="alias-search-bar" @click="searchClick">
				<text class="xyicon xyicon-search m-r-sm"></text>
				<text>请输入关键字</text>
			</view>
		</uni-nav-bar>
		<!-- 头部导航 -->
		<view class="top-tab pos-r" v-show="icateList.length > 0">
			<xy-tabs :list="icateList" :showActive="false" v-model="tabIndex" @tab-click="tabClick"></xy-tabs>
			<view class="all-icate bg-white" @click="goIcate">
				<text class="xyicon xyicon-more-o"></text>
			</view>
		</view>
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<uni-swiper-dot
				:info="carouselList"
				:current="swiperCurrent"
				field="title" mode="round"
				:dotsStyles="{backgroundColor: 'rgba(255, 255, 255, 0.5)', selectedBackgroundColor: 'rgba(255, 255, 255, 1)', selectedBorder: 'none', border: 'none'}">
			    <swiper class="carousel" circular @change="swiperChange">
			    	<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="navTo('/pages/' + item.url)">
			    		<xy-image :src="item.cover" />
			    	</swiper-item>
			    </swiper>
			</uni-swiper-dot>
		</view>
		<!-- 分类 -->
		<view class="cate-section">
			<navigator
				class="cate-item-wrap"
				:key="key_entry"
				v-for="(item,key_entry) in entryList"
				:url="'/pages/' + item.url">
				<view class="cate-item">
					<xy-image :src="item.logo"></xy-image>
					<view class="title">{{item.title}}</view>
				</view>
			</navigator>
		</view>
		<view class="ad-1 hidden">
			<navigator :url="adA1List[0].url" class="ad-nav">
				<xy-image :src="adA1List[0].cover" mode="scaleToFill"></xy-image>
			</navigator>
		</view>
		<!-- 秒杀楼层 -->
		<view class="seckill-section m-t-sm" v-show="$store.state.app.siteInfo.versionFuncsAll.indexOf('ishop_seckill') >= 0">
			<navigator class="s-header" url="/pages/ishop_seckill/seckill/lists">
				<text class="title">限时秒杀</text>
				<text class="tip">{{seckillCurrentHour}}点场</text>
				<uni-countdown class="timer" color="#fff" :show-day="false" background-color="#333" :second="seckillLeftSeconds">
				</uni-countdown>
				<text class="xyicon xyicon-right"></text>
			</navigator>
			<scroll-view class="floor-list" scroll-x>
				<view class="scoll-wrapper">
					<navigator :url="'/pages/ishop/item/info?id=' + item.itemId + '&promoteType=1&promoteId=' + item.id"
						v-for="(item, index) in seckillList" :key="index"
						class="floor-item">
						<xy-image :src="item.cover" mode="aspectFill"></xy-image>
						<text class="title clamp">{{item.title}}</text>
						<text class="price">￥{{item.curPrice}}</text>
					</navigator>
				</view>
			</scroll-view>
		</view>

		<!-- 团购楼层 -->
		<view class="f-header m-t-sm" v-show="$store.state.app.siteInfo.versionFuncsAll.indexOf('ishop_group') >= 0">
			<image src="/static/h1.png"></image>
			<view class="tit-box">
				<text class="tit">精品拼团</text>
				<text class="tit2">Boutique Group Buying</text>
			</view>
			<text class="xyicon xyicon-right"></text>
		</view>
		<view class="group-section" v-show="$store.state.app.siteInfo.versionFuncsAll.indexOf('ishop_group') >= 0">
			<swiper class="g-swiper" :duration="500">
				<swiper-item
					class="g-swiper-item"
					v-for="(item, index) in groupList" :key="index"
					v-if="index%2 === 0">
					<view class="g-item left">
						<image :src="item.cover" mode="aspectFill"></image>
						<view class="t-box">
							<text class="title clamp">{{item.title}}</text>
							<view class="price-box">
								<text class="price">￥{{item.curPrice}}</text>
								<text class="m-price">￥188</text>
							</view>
							<view class="pro-box">
							  	<view class="progress-box">
							  		<progress percent="72" activeColor="#fa436a" active stroke-width="6" />
							  	</view>
								<text>6人成团</text>
							</view>
						</view>
					</view>
					<view class="g-item right" v-if="groupList[index+1]">
						<image :src="groupList[index+1].cover" mode="aspectFill"></image>
						<view class="t-box">
							<text class="title clamp">{{groupList[index+1].title}}</text>
							<view class="price-box">
								<text class="price">￥{{groupList[index+1].curPrice}}</text>
								<text class="m-price">￥188</text>
							</view>
							<view class="pro-box">
							  	<view class="progress-box">
							  		<progress percent="72" activeColor="#fa436a" active stroke-width="6" />
							  	</view>
								<text>10人成团</text>
							</view>
						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<!-- 热门商品 -->
		<view class="floor-row m-t-sm">
			<navigator url="/pages/ishop/item/lists?iadTag=home_hot">
				<view class="f-header floor-title">
					<view class="tit-box">
						<text class="tit">热门商品</text>
					</view>
					<text class="xyicon icon-you"></text>
				</view>
			</navigator>
			<view class="item-list-section">
				<view
					v-for="(item, index) in hotList" :key="index"
					class="guess-item"
					@click="navToDetailPage(item)">
					<view class="image-wrapper">
						<xy-image :src="item.cover" mode="aspectFill"></xy-image>
					</view>
					<text class="title clamp">{{item.title}}</text>
					<text class="price">￥{{item.price}}</text>
				</view>
			</view>
		</view>

		<!-- 推荐商品 -->
		<view class="floor-row m-t-sm">
			<navigator url="/pages/ishop/item/lists?iadTag=home_rec">
			<view class="f-header floor-title">
				<view class="tit-box">
					<text class="tit">推荐商品</text>
				</view>
				<text class="xyicon icon-you"></text>
			</view>
			</navigator>

			<view class="item-list-section">
				<view
					v-for="(item, index) in recList" :key="index"
					class="guess-item"
					@click="navToDetailPage(item)">
					<view class="image-wrapper">
						<xy-image :src="item.cover" mode="aspectFill"></xy-image>
					</view>
					<text class="title clamp">{{item.title}}</text>
					<text class="price">￥{{item.price}}</text>
				</view>
			</view>
		</view>
		<!-- #ifdef H5 -->
		<view>
			<a href="http://www.beian.miit.gov.cn/" target="_blank">{{app.siteInfo.icp}}</a>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	
	import {mapState, mapMutations} from 'vuex';
	export default {
		data() {
			return {
				icateList: [],
				tabIndex: 0,
				swiperCurrent: 0,
				carouselList: [],
				entryList: [],
				adA1List: [
					{
						cover: '',
						url: ''
					}
				],
				seckillList: [],
				seckillCurrentHour: '-1',
				seckillLeftSeconds: 60,
				groupList: [],
				hotList:[],
				recList:[],
				goodsList: []
			};
		},
		onLoad() {
			console.log('runEnv:' + this.app.runEnv);
			this.loadData();
		},
		async onShow() {
			if (this.$store.state.app.siteInfo.versionFuncsAll.indexOf('ishop_seckill') >= 0) {
				var myDate = new Date();
				let seckillCurrentHour = myDate.getHours();
				if (seckillCurrentHour != this.seckillCurrentHour) {
					this.seckillCurrentHour = seckillCurrentHour;
					this.seckillLeftSeconds = 3600 - myDate.getMinutes() * 60 - myDate.getSeconds() - 1;
					// 获取当前场次的秒杀列表
					let res = await this.util.request({
						url: '/v1/ishop_seckill/seckill/lists',
						method: 'get',
					});
					if (res.code == 200) {
						this.seckillList = res.data.dataList;
					}
				}
			}
		},
		computed: {
			...mapState(['app', 'user'])
		},
		async onPullDownRefresh() {
			await this.loadData();
			uni.stopPullDownRefresh();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 5000);
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {// 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: this.$store.state.app.siteInfo.title,
				path: '/pages/home'
			}
		},
		methods: {
			tabClick(index) {
				if (index > 0) {
					let icateId = this.icateList[index].id;
					uni.navigateTo({
						url: `/pages/ishop/item/lists?icateId=${icateId}`
					})
				}
			},
			searchClick() {
				uni.navigateTo({url: '/pages/search/search'});
			},
			goIcate() {
				uni.switchTab({url: '/pages/ishop/icate/trees'});
			},
			// 加载数据
			async loadData() {
				// 所有品类
				let re = await this.util.request({
					url: '/v1/ishop/icate/lists',
					method: 'get',
					data: {
						level: 1
					}
				});
				if (re.code == 200) {
					let home = [
						{
							title: '首页'
						}
					];
					this.icateList = home.concat(re.data.dataList);
				}

				// 首页轮播图
				let res = await this.util.request({
					url: '/v1/ishop/slider/lists',
					method: 'get',
				});
				if (res.code == 200) {
					this.carouselList = res.data.dataList;
				}

				// 首页功能入口
				let res1 = await this.util.request({
					url: '/v1/ishop/entry/lists',
					method: 'get',
				});
				if (res1.code == 200) {
					this.entryList = res1.data.dataList;
				}

				// A1广告
				// let res2 = await this.util.request({
				// 	url: '/v1/ishop/ad/lists/A1',
				// 	method: 'get',
				// });
				// if (res2.code == 200) {
				// 	this.adA1List = res2.data.dataList;
				// }

				// 热门商品
				let res3 = await this.util.request({
					url: '/v1/ishop/item/lists',
					method: 'get',
					data: {
						limit: 4,
						iadTag: 'home_hot'
					}
				});
				if (res3.code == 200) {
					this.hotList = res3.data.dataList;
				}

				// 推荐商品
				let res4 = await this.util.request({
					url: '/v1/ishop/item/lists',
					method: 'get',
					data: {
						limit: 4,
						iadTag: 'home_rec'
					}
				});
				if (res4.code == 200) {
					this.recList = res4.data.dataList;
				}
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
			},
			// 跳转
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			//详情页
			navToDetailPage(item) {
				let url = item.url;
				uni.navigateTo({
					url: '/pages/ishop/item/info?' + 'id=' + item.id
				})
			},
		}
	}
</script>

<style lang="scss">
	/* uni-ui样式美化 */
	/deep/ .uni-navbar {
		.uni-navbar__header-btns-left {
			width: auto !important;
			.uni-navbar-btn-text uni-text {
				font-weight: 700;
				color: $uni-color-primary !important;
			}
		}
	}
	
	/* 顶部Tab */
	.top-tab {
		padding-right: 34px;
		overflow: hidden;
		box-sizing: content-box;
		.all-icate {
			display: flex;
			align-items: center;
			top: 0;
			bottom: 0;
			right:0;
			width: 34px;
			height: 100%;
			padding-left: 10px;
			position: absolute;
			box-shadow: 1px 2px 5px #ccc;
		}
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		.carousel {
			width: 100%;
			height: 150px;
			.carousel-item {
				width: 100%;
				height: 100%;
				padding: 0 0px;
				overflow: hidden;
			}
			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	/* 分类 */
	.cate-section {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap:wrap;
		padding: 15px 11px 0px;
		background: #fff;
		.cate-item-wrap {
			width: 20%;
			margin-bottom: 8px;
		}
		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-bottom: 10px;
			.title {
				margin-top: 5px;
				font-size: 13px;
				color: #333;
			}
		}
		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 40px;
			height: 40px;
			margin-bottom: 7px;
			border-radius: 50%;
			opacity: .7;
			box-shadow: 2px 2px 10px rgba(250, 67, 106, 0.3);
		}
	}
	.ad-1{
		width: 100%;
		padding: 5px 0;
		background: #fff;
		.ad-nav {
			width: 100%;
			height: 105px;
		}
		image{
			width: 100%;
			height: 100%;
		}
	}
	/* 秒杀专区 */
	@font-face {
	  font-family: 'seckillTitle';
	  font-display: swap;
	  src: url('//at.alicdn.com/t/webfont_5cd1ip2w78m.eot'); /* IE9*/
	  src: url('//at.alicdn.com/t/webfont_5cd1ip2w78m.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
	  url('//at.alicdn.com/t/webfont_5cd1ip2w78m.woff2') format('woff2'),
	  url('//at.alicdn.com/t/webfont_5cd1ip2w78m.woff') format('woff'), /* chrome、firefox */
	  url('//at.alicdn.com/t/webfont_5cd1ip2w78m.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/
	  url('//at.alicdn.com/t/webfont_5cd1ip2w78m.svg#阿里汉仪智能黑体') format('svg'); /* iOS 4.1- */
	}
	.seckill-section{
		padding: 4px 15px 4px;
		background: #fff;
		.s-header{
			display:flex;
			align-items:center;
			height: 46px;
			line-height: 1;
			.title {
				color: $uni-color-primary;
				font-size: 18px;
				font-family: 'seckillTitle';
			}
			.tip {
				font-size: $font-base;
				color: #777;
				margin: 0 12px 0 20px;
			}
			.timer {
				/deep/ .uni-countdown__number {
					height: 18px;
					line-height: 18px;
					margin: 0;
					padding: 0 3px
				}
				/deep/ .uni-countdown__splitor {
					line-height: 1;
				}
			}
			.xyicon-right {
				font-size: $font-lg;
				color: $font-color-light;
				flex: 1;
				text-align: right;
			}
		}
		.floor-list{
			white-space: nowrap;
		}
		.scoll-wrapper{
			display:flex;
			align-items: flex-start;
		}
		.floor-item{
			width: 85px;
			margin-right: 10px;
			font-size: 14px;
			color: #333;
			line-height: 1.4;
			image {
				width: 85px;
				height: 85px;
				border-radius: 3px;
			}
			.price{
				font-size: 12px;
				font-weight: 700;
				color: $uni-color-primary;
			}
		}
	}

	.f-header {
		display:flex;
		align-items:center;
		height: 50px;
		padding: 3px 15px 4px;
		background: #fff;
		image{
			flex-shrink: 0;
			width: 40px;
			height: 40px;
			margin-right: 10px;
		}
		&.floor-title {
			height: 40px;
		}
		.tit-box{
			flex: 1;
			display: flex;
			flex-direction: column;
		}
		.tit{
			font-size: 17px;
			color: #font-color-dark;
			line-height: 1.3;
		}
		.tit2{
			font-size: 16px;
			color: $font-color-light;
		}
		.icon-you{
			font-size: 17px;
			color: $font-color-light;
		}
	}

	/* 团购楼层 */
	.group-section{
		background: #fff;
		.g-swiper{
			height: 225px;
			padding-bottom: 15px;
		}
		.g-swiper-item{
			width: 100%;
			padding: 0 15px;
			display:flex;
		}
		image{
			width: 100%;
			// height: 130px;
			border-radius: 4px;
		}
		.g-item{
			display:flex;
			flex-direction: column;
			overflow:hidden;
		}
		.left{
			flex: 0.5;
			margin-right: 12px;
			.t-box{
				padding-top: 10px;
			}
		}
		.right{
			flex: 0.5;
			flex-direction: column-reverse;
			.t-box{
				padding-bottom: 10px;
			}
		}
		.t-box{
			height: 80px;
			font-size: 15px;
			color: $font-color-dark;
			line-height: 1.6;
		}
		.price{
			color:$uni-color-primary;
		}
		.m-price{
			font-size: 13px;
			text-decoration: line-through;
			color: $font-color-light;
			margin-left: 4px;
		}
		.pro-box{
			display:flex;
			align-items:center;
			margin-top: 5px;
			font-size: $font-sm;
			color: $font-base;
			padding-right: 5px;
		}
		.progress-box{
			flex: 1;
			border-radius: 10px;
			overflow: hidden;
			margin-right: 8upx;
		}
	}
</style>
